<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
  <title>fetch (CacheFetcher)</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
</head>
<body class="standalone-code">
  <pre><span class="ruby-comment cmt"># File lib/cache_fetcher.rb, line 12</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">fetch</span>(<span class="ruby-identifier">url</span>)
      <span class="ruby-identifier">file</span> = <span class="ruby-constant">MD5</span>.<span class="ruby-identifier">hexdigest</span>(<span class="ruby-identifier">url</span>)
      <span class="ruby-identifier">file_path</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;&quot;</span>, <span class="ruby-ivar">@cache_dir</span>, <span class="ruby-identifier">file</span>)
      <span class="ruby-comment cmt"># we check if the file -- a MD5 hexdigest of the URL -- exists</span>
      <span class="ruby-comment cmt">#  in the dir. If it does we just read data from the file and return</span>
      <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-constant">File</span>.<span class="ruby-identifier">exists?</span> <span class="ruby-identifier">file_path</span>
        <span class="ruby-comment cmt">#puts 'Not found in cache'</span>
        <span class="ruby-comment cmt"># if the file does not exist (or if the data is not fresh), we</span>
        <span class="ruby-comment cmt">#  make an HTTP request and save it to a file</span>
        <span class="ruby-comment cmt">#puts 'Fetching file from internet...'</span>
        <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">file_path</span>, <span class="ruby-value str">&quot;w&quot;</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">data</span><span class="ruby-operator">|</span>
           <span class="ruby-identifier">data</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-constant">Net</span><span class="ruby-operator">::</span><span class="ruby-constant">HTTP</span>.<span class="ruby-identifier">get_response</span>(<span class="ruby-constant">URI</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">url</span>)).<span class="ruby-identifier">body</span>
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">else</span>
        <span class="ruby-comment cmt">#puts 'Using cache'</span>
      <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">return</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">file_path</span>)
   <span class="ruby-keyword kw">end</span></pre>
</body>
</html>